package com.zxy.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.zxy.demo.intercept.LoginIntercept;

/**
 * SpringMvc扩展配置类
 * @author ZENG.XIAO.YAN
 * @blog  http://www.cnblogs.com/zeng1994/
 * @version 1.0
 */

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
	// WebMvcConfigurerAdapter 这个类在SpringBoot2.0已过时，官方推荐直接实现WebMvcConfigurer 这个接口
	
	// 添加拦截器
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		// 注册登录拦截器
		// 特别说明：SpringBoot2.0之后，拦截器会把静态资源文件也拦截掉
		registry.addInterceptor(loginIntercept()).addPathPatterns("/**")
			.excludePathPatterns("/css/**","/js/**","/img/**")	// 放行静态资源
			.excludePathPatterns("/login")
			.excludePathPatterns("/toLogin")
			.excludePathPatterns("/error")
			.excludePathPatterns("/myCustomError")
			.excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**") // 放行swagger
			.excludePathPatterns("/test/**");	//把我写的测试controller也放行，避免每次搞测试都要登录
	}
	
	/* 这样配置才能在LoginIntercept 中使用@Autowired */
	@Bean
	public LoginIntercept loginIntercept() {
		return new LoginIntercept();
	}
}
